// 模拟后端动态生成路由
import { defineFakeRoute } from 'vite-plugin-fake-server/client'
//import routesConfig from '@/route'
const routesConfig = [
  {
    path: '/',
    name: 'Index',
    meta: {
      title: '首页',
    },

    children: [
      {
        path: '/home',
        name: 'Home',
        meta: {
          title: '首页',
        },
      },
    ],
  },
  {
    path: '/web-gl',
    name: 'WebGL',

    meta: {
      title: 'webgl',
    },

    children: [
      {
        path: '/web-gl/meta',
        name: 'WebGLMeta',

        meta: {
          title: '元宇宙',
        },
      },
    ],
  },
  {
    path: '/blog',
    name: 'Blog',
    meta: {
      title: '博客',
    },
    redirect: '/blog/list',
    children: [
      {
        path: '/blog/list',
        name: 'BlogList',
        meta: {
          title: '博客列表',
        },
      },
    ],
  },
]
const routerData = removeComponentKey(routesConfig)
// 使用递归来移除 component 属性
function removeComponentKey(routes) {
  return routes.map(route => {
    // 创建一个新的对象，避免直接修改原数组
    const { component, children, ...rest } = route

    // 如果存在 children，则递归移除 children 中的 component 属性
    return {
      ...rest,
      ...(children ? { children: removeComponentKey(children) } : {}),
    }
  })
}

export default defineFakeRoute([
  {
    url: '/get-async-routes',
    method: 'get',
    response: () => {
      return {
        success: true,
        code: 200,
        message: '',
        data: routerData,
      }
    },
  },
])
